package com.zhifou.config;

import com.zhifou.handler.AccessInterceptor;
import com.zhifou.handler.CurrentUserMethodArgumentResolver;
import com.zhifou.handler.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author RedissonConfig
 * @description 注册拦截器
 * @date 2023-02-23 10:01
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private RequestInterceptor requestInterceptor;
    @Resource
    private AccessInterceptor accessInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 请求拦截器
        registry.addInterceptor(requestInterceptor)
                .addPathPatterns("/**").excludePathPatterns("/user/login");
        //第二个拦截器
        registry.addInterceptor(accessInterceptor)
                .addPathPatterns("/**").excludePathPatterns("/user/login");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver());
    }


    @Bean
    public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
        return new CurrentUserMethodArgumentResolver();
    }
}

